package com.jy.modules.cims.settle.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jy.modules.cims.common.controller.CimsBaseController;
import com.jy.modules.cims.common.sysUtils.ExceptionUtils;
import com.jy.modules.cims.common.sysUtils.SimpleStringUtils;
import com.jy.modules.cims.creditright.service.SecondBuyCreditService;
import com.jy.modules.cims.data.dto.DealDto;
import com.jy.modules.cims.data.dto.QuotaCreditRightDto;
import com.jy.platform.core.message.DataMsg;

/**
 * 二次购买处理控制层
 */
@Controller
@Scope("prototype")
@RequestMapping("/secondBuyCredit")
public class SecondBuyCreditController extends CimsBaseController {
	private static final Logger logger = LoggerFactory.getLogger(SecondBuyCreditController.class);

	public SecondBuyCreditController() {
	}

	@Autowired
	@Qualifier("com.jy.modules.cims.creditright.service.SecondBuyCreditService")
	private SecondBuyCreditService secondBuyCreditService;

	/**
	 * 二次购买--从额度申请债权明细挑选债权 更新债券信息交易状态，申请>>交易
	 */
	@RequestMapping(value = "/insertOrUpdateCreditBuy")
	@ResponseBody
	public DataMsg insertOrUpdateCreditBuy(HttpServletRequest request, QuotaCreditRightDto dto, @ModelAttribute DataMsg dataMsg) throws Exception {
		try {
			dto = (QuotaCreditRightDto) super.initDto(dto);
			String dealCode = dto.getDealCode();
			logger.info("交易流水号::" + dealCode);

			dataMsg = super.initDataMsg(dataMsg);
			Map<String, Object> resultMap = new HashMap<String, Object>();
			if (SimpleStringUtils.hasText(dealCode)) {
				logger.info("==================二次购买修改==================");
				secondBuyCreditService.updateCreditBuy(dto);
				dataMsg.setMsg("修改成功");
			} else {
				logger.info("==================二次购买新增==================");
				String settleDealCodes = this.getParameterString("settleDealCodes");// 结算交易流水号
				dto.setSettleDealCodes(settleDealCodes);
				secondBuyCreditService.insertCreditBuy(dto);
				dataMsg.setMsg("新增成功");
			}

			resultMap.put("id", dto.getDealId());
			resultMap.put("dealCode", dto.getDealCode());

			dataMsg.setData(resultMap);
		} catch (Exception e) {
			dataMsg.failed(e.getMessage());
			logger.error("执行方法SecondBuyCreditController.insertOrUpdateCreditBuy异常：", ExceptionUtils.getStackTraceAsString(e));
		}
		return dataMsg;
	}

	/**
	 * @throws Exception
	 * @Title save
	 * @Description 保存交易流水表，创建债权包，更新债权或贷款的包号(操作交易流水表、债权包表、债权明细、贷款明细)
	 * @param request
	 * @param response
	 * @param result
	 * @author 
	 * @date 2015-1-20-上午9:45:29
	 * @update
	 * @throws
	 */
	@RequestMapping(value = "/doSaveConfirm")
	@ResponseBody
	public DataMsg doSaveConfirm(HttpServletRequest request, DealDto dto, @ModelAttribute DataMsg dataMsg) throws Exception {
		try {
			dto = (DealDto) super.initDto(dto);
			secondBuyCreditService.doConfirm(dto);
			
			dataMsg = super.initDataMsg(dataMsg);
			dataMsg.setMsg("保存成功");
			dataMsg.setData(this.makeJSONData(dto.getId()));
		} catch (Exception e) {
			dataMsg.failed(e.getMessage());
			logger.error("执行方法SecondBuyCreditController.doSaveConfirm异常：", e);
			throw new Exception(e.getMessage());
		}
		return dataMsg;
	}
	
	/**
	 * 确认二次购买的债权
	 * @Title doConfirmSecondBuyCredit
	 * @Description TODO
	 * @param request
	 * @param dto
	 * @param dataMsg
	 * @return
	 * @throws Exception 
	 * DataMsg 
	 * @author lizhiqiang
	 * @date 2015-9-22-下午4:29:20
	 * @update
	 * @throws
	 */
	@RequestMapping(value = "/doConfirmSecondBuyCredit")
	@ResponseBody
	public DataMsg doConfirmSecondBuyCredit(HttpServletRequest request, DealDto dto, @ModelAttribute DataMsg dataMsg) throws Exception {
		try {
			dto = (DealDto) super.initDto(dto);
			
			secondBuyCreditService.doConfirmSecondBuyCreditAll(dto);
			
			dataMsg = super.initDataMsg(dataMsg);
			dataMsg.setMsg("确认成功");
		} catch (Exception e) {
			dataMsg.failed(ExceptionUtils.getStackTraceAsString(e));
			logger.error("执行方法SecondBuyCreditController.doConfirmSecondBuyCredit异常：", ExceptionUtils.getStackTraceAsString(e));
		}
		return dataMsg;
	}

}